/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuplestoremasterserver;

import core.IServerCallBack;
import core.ProtocolSD;
import core.ProtocolSD.Operation;
import java.io.IOException;
import java.nio.channels.SelectionKey;


/**
 *
 * @author Jorge
 */
public class ServerClientListener implements ProtocolSD.IProtocolListener {

    private Operation operation;
    private IServerCallBack callback;
    private boolean close = false;
    private Long avalilableSize = null;
    private Integer comunicationPort = 0;
    private Long keySize = null;
    private String serverAddress = null;
    private Integer serverPort = null;
    private Integer uniqueId = 0;
    private Long usedSize = null;

    public void onReadHeader(int operation, int numArgs, ProtocolSD protocolSD) {
        this.operation = Operation.values()[operation];
        callback.onReadHeader(operation, numArgs, protocolSD);
    }

    public void onReadArgument(byte[] data, ProtocolSD protocolSD) {
        switch (operation) {
            case REMOVE:
                break;
            case NOTIFY:
                break;
            case STATUS:
                break;
            case KEYSET:
                break;
        }
        callback.onReadArgument(data, protocolSD);
    }

    public void onReadArgumentSize(int size, ProtocolSD protocolSD) {
        callback.onReadArgumentSize(size, protocolSD);
    }

    public void onReadAllArguments(ProtocolSD protocolSD) {
        protocolSD.getKey().interestOps(SelectionKey.OP_WRITE);
        switch (operation) {
            case REMOVE:
                break;
            case NOTIFY:
                break;
            case STATUS:
                break;
            case KEYSET: 
                break;
        }

        close = true;
        try {
            protocolSD.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
        callback.onFinish();
        callback.onReadAllArguments(protocolSD);
    }

    public void setCallBack(IServerCallBack callback) {
        this.callback = callback;
    }

    public IServerCallBack getCallBack() {
        return callback;
    }

    public void onWriteComplete(ProtocolSD protocolSD) {
        protocolSD.getKey().interestOps(SelectionKey.OP_READ);
        callback.onWriteComplete(protocolSD);
    }

    public void onAcceptConnect() {
        callback.onFinishConnect();
    }

    public void onReadParcialParam(byte[] readedBytes, int tmpParcial, int remaning, ProtocolSD protocolSD) {
        callback.onReadParcialParam(readedBytes, tmpParcial, remaning);
    }

    public boolean isClose() {
        return close;
    }
}
